<?php
session_start();
require_once '../config/sys_config.php';
header("Content-type: text/html; charset=utf-8"); 
require_once('conn.php');
require_once('inc/admin_act.php');
require_once MB_CTRL_DIR.'/base_mod.class.php';
require_once MB_APP_DIR.'/global.class.php';
$group = new MBAdminGroup($host,$root,$pwd,$db);

try{
	$logger = new MBLog($mbConfig["log_path"], 'admin');
	MBGlobal::setLogger($logger);
	$base = new BaseMod();
	$base->checkLogin();
	$api = MBGlobal::getApiClient();
}catch(MBException $e){
	header('Location: error.php?e=10000');
	exit();
}

$act = $_GET['a'];
$model = $_GET['m'];
$url = $_SERVER['HTTP_REFERER'];
$_SESSION["bkurl"] = $url;
switch($act){
		case 'gp':
			switch($model){
				case 'add':
					$gname = $_POST['gname'];
					$status = $_POST['status'];
					$priority = $_POST['priority'];
					if($gname && in_array($status,array(0,1))  && $priority){
						$param = array(
							'g_name' => $gname,
							'g_status' => intval($status),
							'g_priority' => intval($priority),
							'g_add_time' => time()	
						);		
						$ret = $group->addGroup($param,'g_name');
						if($ret==1){
							$group->crfile('group');
							header('Location: suc.php');
							exit();
						}elseif($ret==104){
							header('Location: error.php?e=104');	
							exit();
						}else{
							header('Location: error.php?e=101');	
							exit();
						}			
					}else{
						header('Location: error.php?e=102');	
						exit();
					}
					break;
				case 'del':
					$id= (int) $_POST['id'];
					$ret = $group->delGroup($id);
					break;
				case 'edit':
					$gname = $_POST['gname'];
					$status = $_POST['status'];
					$priority = $_POST['priority'];
					$gid = intval($_POST['gid']);
					if($gname && in_array($status,array(0,1))  && $priority && $gid){
						$param = array(
							'g_name' => $gname,
							'g_status' => intval($status),
							'g_priority' => intval($priority),
							'g_add_time' => time()	
						);		
						$ret = $group->editGroup($gid,$param);
						if($ret==109){
							header('Location: error.php?e=109');	
							exit();
						}elseif($ret>=0){
							header('Location: suc.php');
							exit();
						}else{
							header('Location: error.php?e=101');	
							exit();
						}
					}else{
						header('Location: error.php?e=102');	
						exit();
					}	
					break;
			}		
			break;		
		case 'gu':
			switch($model){
				case 'add':
					$uname = $_POST['uname'];
					$info = $_POST['info'];
					$gid = $_POST['g_id'];
					$priority = $_POST['priority'];
					if($uname && $gid  && $priority &&$info){
						$param = array(
							'u_name' => $uname,
							'g_id' => intval($gid),
							'u_priority' => intval($priority),
							'u_info' => $info,
							'u_add_time' => time()	
						);		
						$ret = $group->putUserToGroup($param);
						if($ret==1){
							//$group->crfile('group_user');
							header('Location: suc.php');
							exit();
						}elseif($ret==104){
							header('Location: error.php?e=104');	
							exit();
						}else{
							header('Location: error.php?e=101');	
							exit();
						}			
					}else{
						header('Location: error.php?e=102');	
						exit();
					}
					break;
				case 'del':
					$id= (int) $_POST['id'];
					$ret = $group->deleteUserFromGroup($id);
					break;
				case 'edit':
                    $rid = $_POST['rid'];
					$uname = $_POST['uname'];
					$info = $_POST['info'];
					$gid = $_POST['g_id'];
					$priority = $_POST['priority'];

					if($rid && $uname && $gid  && $priority &&$info){
						$param = array(
							'u_name' => $uname,
							'g_id' => intval($gid),
							'u_priority' => intval($priority),
							'u_info' => $info,
							'u_add_time' => time()	
						);		
						$ret = $group->editGroupUser($rid,$param);
						if($ret==109){
							header('Location: error.php?e=109');	
							exit();
						}elseif($ret>=0){
							header('Location: suc.php');
							exit();
						}else{
							header('Location: error.php?e=101');	
							exit();
						}
					}else{
						header('Location: error.php?e=102');	
						exit();
					}	
					break;
			}		
			break;		
}
?>
